package com.wuyoucar.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TCarinfo;
import com.wuyoucar.entity.TCarinfoSource;
import com.wuyoucar.entity.TOrder;
import com.wuyoucar.entity.TPolicy;
import com.wuyoucar.service.ITOrderService;
import org.apache.commons.lang.time.DateUtils;
import org.jacoco.agent.rt.internal_1f1cc91.core.internal.flow.IFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Jontang
 * 这是订单的所有功能
 * 1.根据用户id查看用户的所有订单
 * 2.修改订单信息
 * 3.放弃投保
 * 4.支付保费
 */
@RestController
@RequestMapping("/order")
public class TOrderController {

    @Autowired
    private ITOrderService itOrderService;

    //这是查询根据用户id查询该用户的所有订单
    @RequestMapping("/findAllOrders/{userid}")
    public ResultBean getAllOrders(@PathVariable("userid") Integer userid) throws ParseException {
        HashMap<String, String> map = new HashMap<>();
        List<TOrder> allOrders = itOrderService.findAllOrders(userid);
        if (allOrders.size() == 0) {
             return new ResultBean(404,"空空如也");
        } else {
            for (TOrder allOrder : allOrders) {
                map.put("vin", allOrder.getVin());
                map.put("carnumber", allOrder.getCarnumber());
                TCarinfoSource tCarinfoSource = itOrderService.findCarInfoSource(map);
                if (tCarinfoSource != null) {
                    allOrder.setCheckstate("审核通过");
                    allOrder.setRemark("订单审核通过,请付款!");
                } else {
                    allOrder.setCheckstate("审核不通过");
                    allOrder.setRemark("您填写车辆信息有误,请查看车辆信息是否有误!");
                }
            }
            return new ResultBean(200, "查询成功", allOrders);
        }
    }

    //这是根据订单ordernumber修改订单信息
    @RequestMapping("/updateOrderByOrdernumber")
    public ResultBean modifyOrder(TOrder order) {
        order.setLastmodifytime(new Date(System.currentTimeMillis()));

        String msg = null;
        int i = itOrderService.modifyOrderByOrderid(order);
        if (i > 0) {
            return new ResultBean(200, "修改成功");
        } else {
            return new ResultBean(400, "修改失败");
        }
    }

    //根据ordernumber移除订单
    @RequestMapping("/cancelOrderByOrdernumber/{ordernumber}")
    public ResultBean cancelOrderByOrderid(@PathVariable("ordernumber") String ordernumber) {
        String msg = null;
        int i = itOrderService.removeOrderByOrderid(ordernumber);

        if (i > 0) {
            return new ResultBean(200, "取消订单成功");
        } else {
            return new ResultBean(400, "取消订单失败");
        }
    }

    //根据订单id查找订单
    @RequestMapping("/findOrderByOrdernumber/{ordernumber}")
    public ResultBean findOrderByOrdernumber(@PathVariable("ordernumber") String ordernumber) {
        return new ResultBean(200, "ok", itOrderService.findOrderByOrderid(ordernumber));
    }

    //支付订单
    @RequestMapping("/payOrder/{ordernumber}")
    public ResultBean payOrderByOrder(@PathVariable("ordernumber") String ordernumber) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        Calendar c=Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR,1); //将当前日期加一年
        String validityDate=sdf.format(c.getTime()); //返回String型的时间dao
        Date expirtime = null;
        try {
            expirtime = sdf.parse(validityDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        TOrder order = new TOrder(ordernumber, 1, date, "订单已支付");
        int i = itOrderService.payOrderByOrderNumber(order);
        TOrder tOrder = itOrderService.findOrderByOrderid(ordernumber);
        TPolicy policy = new TPolicy(tOrder.getInsuredpersonname(), Integer.valueOf(tOrder.getOrderstate()), tOrder.getOrdernumber(), tOrder.getPaytime(), tOrder.getPaytime(), tOrder.getLastmodifytime(), tOrder.getUserid(), expirtime,1,tOrder.getPrice());
        itOrderService.addToPolicy(policy);
        itOrderService.removeOrderByOrderid(ordernumber);
        if (i > 0) {
            return new ResultBean(200, "支付成功");
        } else {
            return new ResultBean(404, "支付未成功");
        }
    }
}
